﻿using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;
using System;


public class EventTriggerListener : UnityEngine.EventSystems.EventTrigger
{
    public Action<PointerEventData> onClick;
    public Action<PointerEventData> onDown;
    public Action<PointerEventData> onEnter;
    public Action<PointerEventData> onExit;
    public Action<PointerEventData> onUp;
    public Action<BaseEventData> onSelect;
    public Action<PointerEventData> onDrag; //滑动
    public Action<PointerEventData> onInitializePotentialDrag;
    public Action<PointerEventData> onBeginDrag;
    public Action<PointerEventData> onEndDrag;
    public Action<BaseEventData> onUpdateSelect;
    public Action<PointerEventData> onScroll;

    static public EventTriggerListener Get(GameObject go)
    {
        EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
        if (listener == null) listener = go.AddComponent<EventTriggerListener>();
        return listener;
    }

    static public EventTriggerListener Add(GameObject go)
    {
        EventTriggerListener listener = Get(go);
        return listener;
    }

    public override void OnPointerClick(PointerEventData eventData)
    {
        if (onClick != null) onClick(eventData);
    }
    public override void OnPointerDown(PointerEventData eventData)
    {
        if (onDown != null) onDown(eventData);
    }
    public override void OnPointerEnter(PointerEventData eventData)
    {
        if (onEnter != null) onEnter(eventData);
    }
    public override void OnPointerExit(PointerEventData eventData)
    {
        if (onExit != null) onExit(eventData);
    }
    public override void OnPointerUp(PointerEventData eventData)
    {
        if (onUp != null) onUp(eventData);
    }
    public override void OnSelect(BaseEventData eventData)
    {
        if (onSelect != null) onSelect(eventData);
    }
    public override void OnUpdateSelected(BaseEventData eventData)
    {
        if (onUpdateSelect != null) onUpdateSelect(eventData);
    }
    public override void OnDrag(PointerEventData eventData)
    {
        if (onDrag != null) onDrag(eventData);
    }
    public override void OnInitializePotentialDrag(PointerEventData eventData)
    {
        if (onInitializePotentialDrag != null) onInitializePotentialDrag(eventData);
    }
    public override void OnBeginDrag(PointerEventData eventData)
    {
        if (onBeginDrag != null) onBeginDrag(eventData);
    }
    public override void OnEndDrag(PointerEventData eventData)
    {
        if (onEndDrag != null) onEndDrag(eventData);
    }
    public override void OnScroll(PointerEventData eventData)
    {
        onScroll?.Invoke(eventData);
    }
}
